/*
 * Yekcomputer (r) CMS
 * Copyright (c) 2009, Yekcomputer. All rights reserved.
 * Author : Babak Taraghi 'babak.taraghi@gmail.com'
 * Code licensed under the BSD License.
 * version: 1.0
 */
var one = {
	
	urls : new Array(),
	validatorExist : false,
	calendarExist : false,
	xinhaExist : false,
	catsScriptExist : false,
	firstRetryTime:10, // change this also at the end of page
	globalTimeout:12000, // change this also at the end of page
	
	showLoading : function(){
		$("#loadingTimer").html("0");
		$("#loadingContainer").show("clip","slow");
		clearInterval(one.loadingTimerId);
		one.loadingTimerId = setInterval(one.loadingTimer,1000);
		one.timer = 0;
	},
	
	loadingTimer : function(){
		one.timer += 1;
		$("#loadingTimer").html(one.timer);
	},
	
	hideLoading : function(){
		clearInterval(one.loadingTimerId);
		$("#loadingContainer").hide("clip","slow");
	},
	
	changeContent : function(url){
		$(function(){
			if(typeof FormValidator != "undefined")
				FormValidator.hideErrors();
			if(one.contentAnimating){
				$("#loadingContainer").effect("pulsate", { times:2 }, 500);
				return;
			}
			one.urls.push(url); // for back address
			one.contentAnimating = true;
			one.showLoading();
			$("#userInfo").stop(true,true);
			one.lastPostURL = url;
			$('#content').stop(true,true);
			$('#content').fadeOut(1000); //wait time
			$.ajax({ // calling server
				//data : {ajaxRequest: true,csrf: $.cookie("tradechem_csrf")},
				cache: false,
				error : one.changeContentFaild,
				success : one.parseContentResponse,
				type : "GET",
				timeout : one.globalTimeout,
				url : url
			 });
		});
	},
	
	changeContentFaild : function(XMLHttpRequest, textStatus, errorThrown){
		one.hideLoading();
		$('#content').stop(true,true);
		$('#content').html("<h1 style='color:#ccc;font-size:20px;'>Whoops! Something went wrong.<br>Please check your internet connection.</h1>" +
				"<p id='retryPara'>Connecting in <span id='retryTimeContainer'>"+one.firstRetryTime+"</span> seconds &hellip; or Try to <a onclick='one.manualTryAgain();' style='font-weight:bold;'>connect now</a></p>" +
				"<br><h1 style='color:#ccc;font-size:20px;'>You can also :</h1>" +
				"<ul><li><a href='"+one.lastPostURL+"' style='font-weight:bold;'>Browse the address</a> you requested for. (Recommended)<br><br></li>" +
				"<li><a onclick='window.location.reload();' style='font-weight:bold;'>Refresh the page</a> and try again.</li></ul>" +
				"</p>");
		$('#content').show("clip",1000);
		one.retryTimeCounter();
		one.tryTimerId = setTimeout(one.tryAgain,(one.firstRetryTime*1000)+800);
	},
	
	retryTimeCounter : function(){
		if($("#retryTimeContainer").text() > 1){
			$("#retryTimeContainer").html($("#retryTimeContainer").text()-1);
			one.timeCounterID = setTimeout(one.retryTimeCounter,1000);
		}else{
			$("#retryPara").html("Trying to reconnect now &hellip;");
		}
	},
	
	manualTryAgain : function(){
		$("#retryPara").html("Trying to reconnect now &hellip;");
		clearTimeout(one.tryTimerId);
		clearTimeout(one.timeCounterID);
		one.tryAgain();
	},
	
	tryAgain : function(){
		if(one.firstRetryTime < 60)
			one.firstRetryTime+=10;
		one.contentAnimating = false;
		if(one.globalTimeout < 54000)
			one.globalTimeout+=7000;
		one.changeContent(one.lastPostURL);
	},
	
	parseContentResponse : function(response){
		if(response == "" || !response || response == null){
			one.changeContentFaild(null, null, null);
			return;
		}
		
		try{
			data = response.split("|-|");
			eval("responseObj = "+data[0]);
		}catch(e){
			one.changeContentFaild(null, null, null);
			return;
		}
		
		one.xinhaNeeded = false;
		
		
		$('#breadCrumb').html(responseObj.breadCrumb);
		document.title = responseObj.title;
		
		
		if(
				(!responseObj.categoriesScript || responseObj.categoriesScript == "") &&
				(!responseObj.validation || responseObj.validation == "") &&
				(!responseObj.calendar || responseObj.calendar == "") &&
				(!responseObj.xinha || responseObj.xinha == "")
		){
			one.setAndShowContent(data[1]);
		};
		
		
		
		// categories script loading
		if(responseObj.categoriesScript){
			if(!one.catsScriptExist){
				$.getScript(BASEURL+"files/cp/javascripts/categories.js",function(){
					if(
							(!responseObj.validation || responseObj.validation == "") &&
							(!responseObj.calendar || responseObj.calendar == "") &&
							(!responseObj.xinha || responseObj.xinha == "")
					){
						one.setAndShowContent(data[1]);
					};
				});
				one.catsScriptExist = true;
			}else{
				if(
						(!responseObj.validation || responseObj.validation == "") &&
						(!responseObj.calendar || responseObj.calendar == "") &&
						(!responseObj.xinha || responseObj.xinha == "")
				){
					one.setAndShowContent(data[1]);
				};
			}
		}
		
		
		
		
		// validation script loading
		if(responseObj.validation){
			if(!one.validatorExist){
				$.getScript(BASEURL+"files/javascripts/validator.js",function(){
					if(
							(!responseObj.calendar || responseObj.calendar == "") &&
							(!responseObj.xinha || responseObj.xinha == "")
					){
						one.setAndShowContent(data[1]);
					};
				});
				one.validatorExist = true;
			}else{
				if(
						(!responseObj.calendar || responseObj.calendar == "") &&
						(!responseObj.xinha || responseObj.xinha == "")
				){
					one.setAndShowContent(data[1]);
				};
			}
		}
		
		
		
		
		
		// calendar script loading
		if(responseObj.calendar){
			if(!one.calendarExist){
				$.getCSS(BASEURL+"files/cp/javascripts/calendar/skins/calendar-blue.css","all");
				$.getScript(BASEURL+"files/cp/javascripts/calendar/jalali.js",function(){
					$.getScript(BASEURL+"files/cp/javascripts/calendar/calendar.js",function(){
						$.getScript(BASEURL+"files/cp/javascripts/calendar/lang/calendar-en.js",function(){
							$.getScript(BASEURL+"files/cp/javascripts/calendar/calendar-setup.js",function(){
								
								// xinha xcript loading
								if(responseObj.xinha){
									if(!one.xinhaExist){
										$.getScript(BASEURL+"files/cp/javascripts/xinha/XinhaConfig.js",function(){
											$.getScript(BASEURL+"files/cp/javascripts/xinha/XinhaLoader.js",function(){
												one.setAndShowContent(data[1]);
											});
										});
										one.xinhaExist = true;
									}else{
										$.getScript(BASEURL+"files/cp/javascripts/xinha/XinhaConfig.js",function(){
											one.setAndShowContent(data[1]);
										});
									}
									one.xinhaNeeded = true;
								}
								
								if(
										(!responseObj.xinha || responseObj.xinha == "")
								){
									one.setAndShowContent(data[1]);
								};
							});
						});
					});
				});
				one.calendarExist = true;
			}else{
				// xinha xcript loading
				if(responseObj.xinha){
					if(!one.xinhaExist){
						$.getScript(BASEURL+"files/cp/javascripts/xinha/XinhaConfig.js",function(){
							$.getScript(BASEURL+"files/cp/javascripts/xinha/XinhaLoader.js",function(){
								one.setAndShowContent(data[1]);
							});
						});
						one.xinhaExist = true;
					}else{
						$.getScript(BASEURL+"files/cp/javascripts/xinha/XinhaConfig.js",function(){
							one.setAndShowContent(data[1]);
						});
					}
					one.xinhaNeeded = true;
				}
				
				if(
						(!responseObj.xinha || responseObj.xinha == "")
				){
					one.setAndShowContent(data[1]);
				};
			}
		}else{
			// xinha xcript loading
			if(responseObj.xinha){
				if(!one.xinhaExist){
					$.getScript(BASEURL+"files/cp/javascripts/xinha/XinhaConfig.js",function(){
						$.getScript(BASEURL+"files/cp/javascripts/xinha/XinhaLoader.js",function(){
							one.setAndShowContent(data[1]);
						});
					});
					one.xinhaExist = true;
				}else{
					$.getScript(BASEURL+"files/cp/javascripts/xinha/XinhaConfig.js",function(){
						one.setAndShowContent(data[1]);
					});
				}
				one.xinhaNeeded = true;
			}
		}

	},
	
	setAndShowContent : function(content){
		$(function(){
			one.firstRetryTime = 10;
			one.globalTimeout = 12000;
			$('#content').stop(true,true);
			$('#content').html(content);
			if(one.xinhaNeeded){
				$('#content').show("clip",500,xinha_init);
				//xinha_init();
			}else{
				$('#content').show("clip",500);
			}
			one.hideLoading();
			one.contentAnimating = false;
		});
	}
};
